import { defineStore } from 'pinia'
import { base64Encode, base64Decode } from "@/utils/crypto"
import { signin, signout, refreshToken, updateUserBaseApi } from "@/service/api/user"
import { getUserInfoMap, getUserBase, getUserSecurity } from "@/service/security/user"
import { _GUID, _STATE, _SECURITY, _ACCESS_TOKEN, _REFRESH_TOKEN } from '@/config/constant'

export const useUserStore = defineStore('user', () => {
	const state = reactive({
		userDepts: [],
		userRoles: [],
		userPosts: [],
		userConfig: {},
		userBase: {},
		userSecurity: {},
	})

	function login(data) {
		return new Promise((resolve, reject) => {
			signin(data).then((res : any) => {
				const result = res.data
				uni.setStorageSync(_ACCESS_TOKEN, base64Encode(result.accessToken))
				uni.setStorageSync(_REFRESH_TOKEN, base64Encode(result.refreshToken))
				resolve(res)
			}).catch((err) => {
				reject(err)
			})
		})
	}

	function logout() {
		return new Promise((resolve, reject) => {
			const storageAccessToken = uni.getStorageSync(_ACCESS_TOKEN)
			if (storageAccessToken) {
				signout({ accessToken: base64Decode(storageAccessToken) }).then((res) => {
					state.userDepts = []
					state.userRoles = []
					state.userBase = {}
					state.userConfig = {}
					state.userSecurity = {}
					uni.clearStorageSync()
					resolve(res)
				}).catch((err) => {
					reject(err)
				})
			}
		})
	}

	function loadUserInfo() {
		return new Promise((resolve, reject) => {
			getUserInfoMap({ params: { guid: _GUID } }).then((res : any) => {
				const result = res.data
				state.userDepts = result.userDepts
				state.userRoles = result.userRoles
				state.userPosts = result.userPosts
				state.userBase = result.userBase
				state.userConfig = result.userConfig
				state.userSecurity = result.userSecurity
				uni.setStorageSync(_STATE, JSON.stringify({ logined: true }))
				resolve(res)
			}).catch((err) => {
				reject(err)
			})
		})
	}

	function loadUserBase() {
		return new Promise((resolve, reject) => {
			getUserBase().then((res : any) => {
				state.userBase = res.data
				resolve(res)
			}).catch((err) => {
				reject(err)
			})
		})
	}

	function loadUserSecurity() {
		return new Promise((resolve, reject) => {
			getUserSecurity().then((res : any) => {
				state.userSecurity = res.data
				resolve(res)
			}).catch((err) => {
				reject(err)
			})
		})
	}

	function tokenRefresh() {
		return new Promise((resolve, reject) => {
			const storageRefreshToken = uni.getStorageSync(_REFRESH_TOKEN)
			if (storageRefreshToken) {
				refreshToken({ params: { refreshToken: base64Decode(storageRefreshToken) } }).then((res : any) => {
					const { data } = res
					uni.setStorageSync(_ACCESS_TOKEN, base64Encode(data.accessToken))
					uni.setStorageSync(_REFRESH_TOKEN, base64Encode(data.refreshToken))
					resolve(res)
				}).catch((err) => {
					reject(err)
				})
			}
		})
	}
	async function updateUserBase(data:any) {
		let res:any = await updateUserBaseApi(data)
		if (res.code === 0) {
			state.userBase = res.data
		}
	}

	return {
		state,
		login,
		logout,
		loadUserInfo,
		loadUserBase,
		loadUserSecurity,
		tokenRefresh,
		updateUserBase
	}
},
	{
		persist: true,
	},
)
